<%@page import="Tools.AccentRemover"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="Entity.Category"%>
<%@page import="Entity.News"%>
<%@page import="DAO.DAO_News"%>

<script src="Assets/js/jquery.tagsinput.min.js"></script>
<script src="Assets/js/ckeditor/ckeditor.js"></script>
<script src="Assets/js/ckeditor/adapters/jquery.js"></script>

<script>
    $(document).ready(function () {
        $('textarea').ckeditor();
        $('#tag_input').tagsInput();
    });

</script>

<%
    String nid = request.getParameter("id");
    DAO_News dao = new DAO_News();
    News news = null;

    String title = "";
    String ncid = "";
    int xncid = 0;
    String content = "";
    String tags = "";

    if (nid != null) {
        int id = Integer.parseInt(nid);
        if (request.getParameter("submit") == null) {
            String sql = "SELECT * FROM News WHERE id = " + id;
            ResultSet rs = dao.getData(sql);
            rs.next();
            title = rs.getString(2);
            xncid = rs.getInt(7);
            content = rs.getString(5);
            tags = rs.getString(6);
        } else {
            title = request.getParameter("title");
            ncid = request.getParameter("catid");
            content = request.getParameter("content");
            tags = request.getParameter("tags");
            if (title == null || content == null) {
                out.print("You have to enter all of the fields");
            } else {
                String xTags = "";
                if (tags != null) {
                    xTags = tags;
                }
                long dateline = System.currentTimeMillis() / 1000L;
                int cid = 0;
                int mid = 1;
                cid = Integer.parseInt(ncid);
                title = new String(title.getBytes("ISO8859_1"), "UTF-8");
                String slug = AccentRemover.toUrlFriendly(title);
                content = new String(content.getBytes("ISO8859_1"), "UTF-8");
                xTags = new String(tags.getBytes("ISO8859_1"), "UTF-8");
                news = new News(id, title, slug, (int) dateline, content, xTags, cid, mid);
                int n = dao.updateNews(news);
                if (n > 0) {
                    response.sendRedirect("Dashboard.jsp");
                }
                dao.closeConnection();
            }
        }
%>        
<form role="form" method="post">
    <input type="hidden" name="id" value="<%=id%>">
    <div class="form-group">
        <label>Title</label>
        <input type="text" class="form-control" name="title" value="<%=title%>">
    </div>
    <div class="form-group">
        <label>Category</label>
        <select name="catid" class="form-control">
            <%
                for (Category item : items) {
                    if (xncid == item.getId()) {
            %>
                        <option selected="selected" value="<%=item.getId()%>"><%=item.getName()%></option>
            <%          continue;
                    }
            %>
            <option value="<%=item.getId()%>"><%=item.getName()%></option>
            <%
                }
            %>
        </select>    
    </div>
    <div class="form-group">
        <label>Content</label>
        <textarea name="content" class="form-control" id="content"><%=content%></textarea>
    </div>
    <div class="form-group">
        <label>Tags</label>
        <input type="text" name="tags" class="form-control" id="tag_input" value="<%=tags%>"  />
    </div>    

    <button type="submit" name="submit">Submit</button>
</form>
<%
    }
%>





